package com.touchfish.material.controller;

import com.touchfish.material.common.BaseResult;
import com.touchfish.material.domain.TGoodsMaterial;
import com.touchfish.material.service.MaterialService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor//lombok注解简化注入
public class MaterialController {


    private final MaterialService materialService;
//    构造器注入
//    public MaterialController(MaterialService materialService) {
//        this.materialService = materialService;
//    }

//    TODO GET /listMaterial 按条件模糊查询材料

    @GetMapping("/listMaterial/{condition}")
    public BaseResult listMaterial(@PathVariable(value = "condition") String condition) {
        List<TGoodsMaterial> materials = materialService.listMaterial(condition);
        return BaseResult.success(materials,materials.size());
    }

    /**
     * TODO DELETE /delMaterial 按条件模糊查询材料
     *
     * @return
     */
    @DeleteMapping("/delMaterial/{condition}")
    public BaseResult delMaterial(@PathVariable(value = "condition") String condition) {
        materialService.delMaterial(condition);
        List<TGoodsMaterial> materials = materialService.getMaterialList();
        return BaseResult.success(materials,materials.size());
    }


    /**
     * TODO GET /getMaterialList 查询全部材料
     */
    @GetMapping("/getMaterialList")
    public BaseResult getMaterialList() {
        List<TGoodsMaterial> materials = materialService.getMaterialList();
        return BaseResult.success(materials,materials.size());
    }

/**
 * TODO POST /updateMaterial 编辑材料
 */
    @PutMapping("/updateMaterial")
    @ResponseBody
    public BaseResult updateMaterial(TGoodsMaterial material){
        materialService.updateMaterial(material);
        List<TGoodsMaterial> materials = materialService.getMaterialList();
        return BaseResult.success(materials,materials.size());
    }

//TODO POST /createMaterial
    @PostMapping("/createMaterial")
    @ResponseBody
    public BaseResult createMaterial(TGoodsMaterial material){
        materialService.createMaterial(material);
        List<TGoodsMaterial> materials = materialService.getMaterialList();
        return BaseResult.success(materials,materials.size());
    }

}
